﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using FFS.BLL;
using FFS.Model;

namespace FFS
{
    public partial class frmLoginUser : Form
    {
        public frmLoginUser()
        {
            InitializeComponent();
        }

        private string actionType = "";

        LoginUserInfo user = new LoginUserInfo();
        List<LoginUserInfo> user_list = new List<LoginUserInfo>();
        LoginUserService userService = new LoginUserService();

        private void tsb_add_Click(object sender, EventArgs e)
        {
            this.actionType = "Add";
            this.txt_LoginUserName.Text = "";
            this.txt_LoginUserPassword.Text = "";
            this.chk_IsLocked.Checked = false;
            this.txt_LoginUserName.Focus();
            this.panel_editLoginUser.Visible = true;
        }

        private void tsb_edit_Click(object sender, EventArgs e)
        {
            this.actionType = "Edit";
            if (listView_LoginUser.SelectedItems.Count == 1)
            {
                this.panel_editLoginUser.Visible = true;
            }
            else
            {
                MessageBox.Show("请选择编辑记录行","提示",MessageBoxButtons.OK,MessageBoxIcon.Warning);
            }
        }

        private void tsb_delete_Click(object sender, EventArgs e)
        {
            if (listView_LoginUser.SelectedItems.Count == 1)
            {
                user.LoginUserNo = listView_LoginUser.SelectedItems[0].SubItems[0].Text;
                user.Input_User = Context._Login_UserName.ToString().Trim();
                userService.ManageLoginUser().DeleteLoginUser(user);
                MessageBox.Show("删除成功","提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
                this.btn_Search_Click(sender,e);
            }
            else
            {
                MessageBox.Show("请选择要删除的记录", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        private void tsb_search_Click(object sender, EventArgs e)
        {
            this.panel_search.Visible = !this.panel_search.Visible;
        }

        private void tsmi_add_Click(object sender, EventArgs e)
        {
            this.tsb_add_Click(sender,e);
        }

        private void tsmi_edit_Click(object sender, EventArgs e)
        {
            this.tsb_edit_Click(sender,e);
        }

        private void tsmi_delete_Click(object sender, EventArgs e)
        {
            this.tsb_delete_Click(sender,e);
        }

        private void tsmi_search_Click(object sender, EventArgs e)
        {
            this.tsb_search_Click(sender,e);
        }

        private void btn_Cancel_Click(object sender, EventArgs e)
        {
            this.panel_editLoginUser.Visible = false;
        }

        private void btn_Save_Click(object sender, EventArgs e)
        {
            string verifyString = "";
            if (this.txt_LoginUserName.Text.Trim() == "")
            {
                verifyString = "登陆用户名称不能为空！";
            }
            if (this.txt_LoginUserPassword.Text.Trim() == "")
            {
                verifyString += "\n登陆用户密码不能为空";
            }

            if (verifyString.Trim() != "")
            {
                MessageBox.Show(verifyString, "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            else
            {
                user.LoginUserName = this.txt_LoginUserName.Text.Trim();
                user.LoginPassword = this.txt_LoginUserPassword.Text.Trim();
                user.LoginIsLocked = this.chk_IsLocked.Checked;
                user.Input_User = Context._Login_UserName;
                if (this.actionType == "Add")
                {
                    if (userService.ManageLoginUser().CreateLoginUser(user) == 1)
                    {
                        this.txt_LoginUserName.Text = "";
                        this.txt_LoginUserPassword.Text = "";
                        this.chk_IsLocked.Checked = false;
                        this.txt_LoginUserName.Focus();
                        MessageBox.Show("添加成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        this.btn_Search_Click(sender,e);
                    }
                    else
                    {
                        MessageBox.Show("添加失败", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    }
                }
                else if (this.actionType == "Edit")
                {
                    userService.ManageLoginUser().ModifyLoginUser(user);
                    MessageBox.Show("编辑成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    this.panel_editLoginUser.Visible = false;
                    this.btn_Search_Click(sender, e);
                }

            }

           
        }

        private void txt_LoginUserName_KeyUp(object sender, KeyEventArgs e)
        {
            this.AcceptButton = this.btn_Save;
        }

        private void txt_LoginUserPassword_KeyUp(object sender, KeyEventArgs e)
        {
            this.AcceptButton = this.btn_Save;
        }

        private void listView_LoginUser_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                if (listView_LoginUser.SelectedItems.Count == 1)
                {
                    this.actionType = "Edit";
                    user.LoginUserNo = this.listView_LoginUser.SelectedItems[0].SubItems[0].Text;
                    this.txt_LoginUserName.Text = this.listView_LoginUser.SelectedItems[0].SubItems[1].Text;
                    this.txt_LoginUserPassword.Text = this.listView_LoginUser.SelectedItems[0].SubItems[2].Text;
                    this.chk_IsLocked.Checked = bool.Parse(this.listView_LoginUser.SelectedItems[0].SubItems[3].Text);
                }
            }
            catch (Exception)
            {

            }
        }

        private void btn_Search_Click(object sender, EventArgs e)
        {
            string searchCondition = " 1=1 ";
            if (this.cmb_searchItem.SelectedIndex == 0)
            {
                if (this.cmb_search_compare.SelectedIndex == 0)
                {
                    searchCondition = " LoginUserName like N'%" + this.txt_searchContent.Text.ToString().Trim() + "%'";
                }
                else
                {
                    searchCondition = " LoginUserName = N'" + this.txt_searchContent.Text.ToString().Trim() + "'";
                }
            }
            this.BindListView(userService.ManageLoginUser().GetLoginUserBySearchCondition(searchCondition));
        }

        private void btn_CancelSearch_Click(object sender, EventArgs e)
        {
            this.panel_search.Visible = false;
        }

        private void BindListView(List<LoginUserInfo> userList)
        {
            listView_LoginUser.Items.Clear();
            foreach (LoginUserInfo user in userList)
            {
                if (user != null)
                { 
                    string[] str = new string[6];
                    str[0] = user.LoginUserNo;
                    str[1] = user.LoginUserName;
                    str[2] = user.LoginPassword;
                    str[3] = user.LoginIsLocked.ToString() ;
                    str[4] = user.Input_Date.ToLongDateString() + " " + user.Input_Date.ToLongTimeString();
                    str[5] = user.Input_User;
                    ListViewItem item = new ListViewItem(str);
                    listView_LoginUser.Items.Add(item);
                }
            }
        }

        private void frmLoginUser_Load(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Maximized;
            this.BindListView(userService.ManageLoginUser().GetAllLoginUser());
            this.cmb_searchItem.SelectedIndex = 0;
            this.cmb_search_compare.SelectedIndex = 0;
            
        }
    }
}
